<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="page-tools">
        <!-- el-tabs 的v-model属性是对应高亮的标签属性 -->
        <el-tabs v-model="activeName">
          <!-- 角色管理 -->
          <el-tab-pane label="角色管理" name="first">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="showDialog = true"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table border="" :data="list">
              <el-table-column label="序号" type="index" width="120" />
              <el-table-column label="角色名称" prop="name" width="240" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <!-- 作用域插槽 -->
                <template slot-scope="{ row }">
                  <el-button size="small" type="success" @click="setAuth(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="edit(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="del(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                :total="page.total"
                :current-page="page.page"
                :page-size="page.pagesize"
                layout="prev,pager,next"
                @current-change="changePage"
              />
            </el-row>
          </el-tab-pane>
          <!-- 公司信息 -->
          <el-tab-pane label="公司信息" name="second">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="info.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="info.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="info.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="info.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <el-dialog
      :title="showtitle"
      :visible.sync="showDialog"
      width="45%"
      :before-close="handleClose"
    >
      <el-form ref="form" :model="roleForm" :rules="rules" label-width="100px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="handleClose">取消</el-button>
          <el-button size="small" type="primary" @click="submit">确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
    <!-- 弹框 -->
    <setAuth ref="authForm" />
  </div>
</template>

<script>
import { getRoleList, companyInfo, deleteRole, getRoleDetail, updateRole, addRole } from '@/api/setting'
import { mapGetters } from 'vuex'
import setAuth from './components/set-auth'
export default {
  components: {
    setAuth
  },
  data() {
    return {
      activeName: 'first', // tab栏当前显示栏属性
      list: [], // 角色数据
      page: {
        page: 1,
        pagesize: 5,
        total: 0 // 记录总数
      },
      info: {}, // 公司信息
      showDialog: false, // 控制弹框显示
      roleForm: { // 弹窗内form表单model属性
        name: '',
        description: ''
      },
      // 校验规则
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    ...mapGetters(['companyId']),
    showtitle() {
      return this.roleForm.id ? '编辑信息' : '新增信息'
    }
  },
  created() {
    this.getRoleList()
    this.getCompanyInfo()
  },
  methods: {
    // 分配权限
    async setAuth(id) {
      await this.$refs.authForm.getAuthDetail()
      await this.$refs.authForm.getAuthRoleDetail(id)
      this.$refs.authForm.showDialog = true
    },
    // 更新信息并回显
    async submit() {
      try {
        await this.$refs.form.validate()
        // 如果有id就是编辑功能
        if (this.roleForm.id) {
          await updateRole(this.roleForm)
          await this.$message.success('信息更新成功')
        } else {
          // 否则是新增功能
          await addRole(this.roleForm)
          this.$refs.form.resetFields()
          this.roleForm = {
            name: '',
            description: ''
          }
          await this.$message.success('添加成功')
        }
        this.getRoleList()

        this.handleClose()
      } catch (error) {
        console.log(error)
      }
      // console.log(res)
    },
    // 编辑信息
    async edit(id) {
      this.showDialog = true
      this.roleForm = await getRoleDetail(id)
      // this.roleForm.description = description
    },
    // 关闭弹窗回调函数
    handleClose() {
      this.roleForm = {
        name: '',
        description: ''
      }
      this.$refs.form.resetFields()
      this.showDialog = false
    },
    // 删除
    async del(id) {
      try {
        await this.$confirm('确认删除该角色吗')
        await deleteRole(id)
        // 判断是否为最后一页或者是否为最后一个数据
        if (this.list.length === 1 && this.page.page !== 1) {
          this.page.page--
        }
        this.getRoleList()
        this.$message.success('删除成功')
      } catch (error) {
        console.log(error)
      }
    },
    // 获取公司信息
    async getCompanyInfo() {
      this.info = await companyInfo(this.companyId)
    },
    // 监听分页变化事件
    changePage(page) {
      this.page.page = page
      this.getRoleList()
    },
    // 获取角色列表
    async getRoleList() {
      const { rows, total } = await getRoleList(this.page)
      // console.log(res)
      this.list = rows
      this.page.total = total
    }
  }
}
</script>

<style lang='scss' scoped>

</style>
